package com.xiong.miaosha.controller;

import com.xiong.miaosha.domain.MiaoshaOrder;
import com.xiong.miaosha.domain.MiaoshaUser;
import com.xiong.miaosha.domain.OrderInfo;
import com.xiong.miaosha.redis.RedisService;
import com.xiong.miaosha.result.CodeMsg;
import com.xiong.miaosha.result.Result;
import com.xiong.miaosha.service.GoodsService;
import com.xiong.miaosha.service.MiaoshaService;
import com.xiong.miaosha.service.MiaoshaUserService;
import com.xiong.miaosha.service.OrderService;
import com.xiong.miaosha.vo.GoodsVo;
import com.xiong.miaosha.vo.OrderDetailVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author wardseptember
 * @create 2020-03-16 10:02
 */
@RequestMapping("/order")
@Controller
public class OrderController {

    private static Logger log = LoggerFactory.getLogger(OrderController.class);

    @Autowired
    MiaoshaUserService miaoshaUserService;

    @Autowired
    RedisService redisService;

    @Autowired
    GoodsService goodsService;

    @Autowired
    OrderService orderService;

    @Autowired
    MiaoshaService miaoshaService;

    @RequestMapping(value = "/detail")
    @ResponseBody
    public Result<OrderDetailVo> miaosha(Model model, MiaoshaUser user, @RequestParam("orderId") long orderId) {
        if (user == null) {
            return Result.error(CodeMsg.SESSION_ERROR);
        }
        OrderInfo order = orderService.getOrderById(orderId);
        if (order == null) {
            return Result.error(CodeMsg.ORDER_NOT_EXIST);
        }
        long goodsId = order.getGoodsId();
        GoodsVo goods = goodsService.getGoodsVoByGoodsId(goodsId);
        OrderDetailVo orderDetailVo = new OrderDetailVo();
        orderDetailVo.setGoods(goods);
        order.setCreateDateSeconds(order.getCreateDate().getTime());
        orderDetailVo.setOrder(order);
        return Result.success(orderDetailVo);
    }

}
